package com.mdd.store.provider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.store.api.StoreBindingApi;
import com.mdd.store.entity.StoreBinding;
import com.mdd.store.service.IStoreBindingService;
import com.mdd.store.vo.StoreVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @Description
 * @Author 幻速
 * @Date 2024/3/20 18:02
 **/
@Service
public class StoreBindingApiProvider implements StoreBindingApi {
    @Resource
    IStoreBindingService iStoreBindingService;
    @Override
    public boolean sava(Long spuId, List<String> storeIds) {
        List<StoreBinding> storeBindings=new ArrayList<>();
        if (Objects.isNull(storeIds)&&storeIds.isEmpty()) {
            return false;
        }
        storeIds.forEach(storeId -> {
            StoreBinding storeBinding=new StoreBinding();
            storeBinding.setSpuId(spuId);
            storeBinding.setStoreId(Long.parseLong(storeId));
            storeBindings.add(storeBinding);
        });
        return iStoreBindingService.saveBatch(storeBindings);
    }

    @Override
    public List<StoreVo> stores(Long spuId) {
        return iStoreBindingService.getBindStoreBySpuId(spuId);
    }

    @Override
    public boolean savaOrDel(Long spuId, List<String> storeIds) {
        LambdaQueryWrapper<StoreBinding> sbqw=new LambdaQueryWrapper<>();
        sbqw.eq(StoreBinding::getSpuId,spuId);
        iStoreBindingService.remove(sbqw);
        //移除为空数据
        List<String> filteredList = storeIds.stream()
                .filter(s -> s != null && !s.isEmpty())
                .collect(Collectors.toList());

        return sava(spuId, filteredList);
    }
}
